"""Affy RNA Degradation

.. helpdoc::
Calculates the degradation of RNA from a set of Affy data.

"""

"""<widgetXML>
    <name>Affy RNA Degradation<!-- [REQUIRED] title --></name>
    <icon>default.png</icon>
    <tags>
        <tag>Affy</tag>
    </tags>
    <summary>Calculates the degradation of RNA from a set of Affy data.</summary>
    <author>
        <authorname>Kyle R Covington</authorname>
        <authorcontact>kyle@red-r.org</authorcontact>
    </author>
</widgetXML>
"""

"""
<name>RNA Deg</name>
<author>Generated using Widget Maker written by Kyle R. Covington</author>
<RFunctions>affy:AffyRNAdeg</RFunctions>
<tags>Affy QC</tags>
<icon></icon>
<inputWidgets></inputWidgets>
<outputWidgets>affy_RedRplotAffyRNADeg, affy_RedRsummaryAffyRNADeg</outputWidgets>
"""
from OWRpy import * 
import redRGUI, signals
#import libraries.affy.signalClasses as signals
#import libraries.affy.signalClasses.DegFit as df
class RedRAffyRNAdeg(OWRpy): 
    settingsList = []
    def __init__(self, **kwargs):
        OWRpy.__init__(self, **kwargs)
        self.require_librarys(["affy"])
        self.setRvariableNames(["AffyRNAdeg"])
        self.data = {}
        self.RFunctionParam_abatch = ''
        
        """.. rrsignals::"""
        self.inputs.addInput('ab', "Affy Batch Object", signals.affy.RAffyBatch, self.processabatch)
        
        """.. rrsignals::"""
        self.outputs.addOutput('rnadeg', "RNA Degradation Object", signals.affy.DegFit)
        
        self.RFunctionParamlog_it_radioButtons =  redRGUI.base.radioButtons(self.controlArea,  label = "Log Transform:", buttons = ['Yes', 'No'], setChecked = 'Yes', orientation = 'horizontal')
        
        
        
        self.RFunctionParamsignif_digits_spinBox =  redRGUI.base.spinBox(self.controlArea,  label = "Summary Significant Digits:", min = 0, max = 5, value = 3)
        
        self.RFunctionParamcols_radioButtons =  redRGUI.base.radioButtons(self.controlArea,  label = "Plot Color:", buttons = ['Yes', 'No'], setChecked = 'Yes')
        
        self.RFunctionParamtransform_comboBox =  redRGUI.base.comboBox(self.controlArea,  label = "Transform Plot:", items = [('shift.scale', "Shift and Scale"), ('shift.only', "Shift only"), ('none', 'Neither')])
        
        tabs = redRGUI.base.tabWidget(self.controlArea)
        summaryTab = tabs.createTabPage('Summary')
        plotTab = tabs.createTabPage('Plot')
        
        self.ROutputWindow = redRGUI.base.textEdit(summaryTab, label = 'Summary')
        
        
        self.plotArea = redRGUI.base.graphicsView(plotTab, label = 'Plot')
        
        
        
        redRGUI.base.commitButton(self.bottomAreaRight, "Commit", callback = self.commitFunction)
    def processabatch(self, data):
        if data:
            self.RFunctionParam_abatch=data.getData()
            #self.data = data
            self.commitFunction()
        else:
            self.RFunctionParam_abatch=''
    def commitFunction(self):
        if unicode(self.RFunctionParam_abatch) == '': return
        injection = []
        if unicode(self.RFunctionParamlog_it_radioButtons.getChecked()) == 'Yes':
            string = 'log.it=TRUE'
            injection.append(string)
        else:
            string = 'log.it=FALSE'
            injection.append(string)

        inj = ','.join(injection)
        self.R(self.Rvariables['AffyRNAdeg']+'<-AffyRNAdeg(abatch='+str(self.RFunctionParam_abatch)+','+inj+')', wantType = 'NoConversion')
        newData = signals.affy.DegFit(self, data = self.Rvariables["AffyRNAdeg"]) # moment of variable creation, no preexisting data set.  To pass forward the data that was received in the input uncomment the next line.
        #newData.copyAllOptinoalData(self.data)  ## note, if you plan to uncomment this please uncomment the call to set self.data in the process statemtn of the data whose attributes you plan to send forward.
        self.rSend("rnadeg", newData)
        self.summaryRNADeg()
        self.plotDeg()
        
    def summaryRNADeg(self):
        injection = []
        
        string = 'signif.digits='+unicode(self.RFunctionParamsignif_digits_spinBox.value())+''
        injection.append(string)
        inj = ','.join(injection)
        self.ROutputWindow.clear()
        tmp = self.R('paste(capture.output('+'summaryAffyRNAdeg(rna.deg.obj='+self.Rvariables['AffyRNAdeg']+','+inj+')), collapse ="\n")')
        self.ROutputWindow.insertPlainText(tmp)
    def plotDeg(self):
        
        injection = []
        if unicode(self.RFunctionParamcols_radioButtons.getChecked()) == 'Yes':
            string = 'cols=rainbow(length('+self.Rvariables['AffyRNAdeg']+'), 0, 1)'
            injection.append(string)
        else:
            string = 'cols = NULL'
            injection.append(string)
        if unicode(self.RFunctionParamtransform_comboBox.currentId()) != 'none':
            text = unicode(self.RFunctionParamtransform_comboBox.currentId())
            string = 'transform=\"'+unicode(text)+'\"'
            injection.append(string)
        
        inj = ','.join(injection)
        
        
        self.plotArea.plot(query = 'rna.deg.obj='+unicode(self.Rvariables['AffyRNAdeg'])+','+inj, function = 'plotAffyRNAdeg')